﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using SilverNotes.Common;

namespace SilverNotes
{
  public partial class MainPage : UserControl
  {
    [ImportMany(typeof(IViewDescriptor))]
    public List<IViewDescriptor> PluggedDescriptors;

    private bool _isDragging;
    private Point _startPoint;
    private UIElement _dragThumb;

    private MainPageViewModel ViewModel
    {
      get { return (MainPageViewModel) DataContext; }
      set { DataContext = value; }
    }

    public MainPage()
    {
      InitializeComponent();
      
      PartInitializer.SatisfyImports(this);
      Loaded += (o, e) =>
                  {
                    ViewModel = new MainPageViewModel(PluggedDescriptors);
                  };
      
    }

    private void PrintClicked(object sender, RoutedEventArgs e)
    {
      var booklet = new Booklet
      {
        Height = PageSettings.HeightA4,
        Width = PageSettings.WidthA4
      };

      booklet.SetPages(_selectedPages.Pages);

      PrintNotes(booklet);
    }

    private static void PrintNotes(UIElement notes)
    {
      var doc = new PrintDocument { DocumentName = "Notes" };
      
      doc.PrintPage += (o, e) =>
                         {
                           e.PageVisual = notes;
                           e.HasMorePages = false;
                         };

      doc.Print();
    }

    private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
      ((FrameworkElement) sender).CaptureMouse();
      _isDragging = true;
      _startPoint = e.GetPosition(null);
    }

    private void OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
      ((FrameworkElement)sender).ReleaseMouseCapture();
      _isDragging = false;
      if (_dragThumb != null)
      {
        _cnvDragLayer.Children.Remove(_dragThumb);
        _dragThumb = null;
      }
      var elements = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), _selectedPages);
      foreach (var candidate in elements)
      {
        if (SelectedPages.GetAcceptDrop(candidate))
        {
          _selectedPages.SetContent(candidate);
        }
      }
    }

    private void OnPreviewMouseMove(object sender, MouseEventArgs e)
    {
      if (!_isDragging)
      {
        return;
      }

      if (_dragThumb == null)
      {
        _dragThumb = CreateDragThumb();
        _cnvDragLayer.Children.Add(_dragThumb);
        Canvas.SetLeft(_dragThumb, _startPoint.X - Constants.PreviewWidth / 2.0);
        Canvas.SetTop(_dragThumb, _startPoint.Y - Constants.PreviewHeight / 2.0);
      }

      var newPos = e.GetPosition(null);
      var x = Canvas.GetLeft(_dragThumb) + newPos.X - _startPoint.X;
      var y = Canvas.GetTop(_dragThumb) + newPos.Y - _startPoint.Y;

      Canvas.SetLeft(_dragThumb, x);
      Canvas.SetTop(_dragThumb, y);

      _startPoint = newPos;
    }

    private FrameworkElement CreateDragThumb()
    {
      var thumb = new Image
                    {
                      Width = Constants.PreviewWidth,
                      Height = Constants.PreviewHeight,
                      Source = new WriteableBitmap(ViewModel.SelectedItem.RootVisual, null)
                    };

      return thumb;
    }
  }
}
